# 此接口为上传mri信息接口

from Flask_run import db
from database.models.mri import Mri
from database.models.patient import Patient

import datetime
def update(info):
  print(info)
  study_id=info["study_id"]
  cheif=info["clinic_info"]["cheif"]
  remark=info["clinic_info"]["remark"]
  date=info["surgery_info"]["date"]
  if date is None or date == "":
    local_date = None
  else:
    if "GMT" in date:
      date = date + "+0800 (CST)"
      date = date.replace(",", "")
      print(date)
      date = date.split(" ")
      a = date[1] 
      date[1] = date[2]
      date[2] = a 
      date = " ".join(date)
      GMT_FORMAT = '%a %b %d %Y %H:%M:%S GMT+0800 (CST)'
      local_date = datetime.datetime.strptime(date, GMT_FORMAT)
    else :  
      print(date)
      utc_date2=datetime.datetime.strptime(date,"%Y-%m-%dT%H:%M:%S.%fZ")
      local_date=utc_date2+datetime.timedelta(hours=8)
      local_date=datetime.datetime.strftime(local_date,"%Y-%m-%d 00:00:00")
  region=info["surgery_info"]["region"]
  zhuw=info["surgery_info"]["zhuw"]
  xinp=info["surgery_info"]["xinp"]
  p_invasive=info["pathology_info"]["p_invasive"]
  p_level=info["pathology_info"]["p_level"]
  vig=info["pathology_info"]["vig"]
  p_sub=info["pathology_info"]["p_sub"]

  try:
    db.session.query(Mri).filter_by(study_id=study_id).update({'complaints': cheif,'notes': remark,'operation_date': local_date,'Lesion_site': region,'arachnoid': zhuw,'simpson': xinp,'patholog_invasion': p_invasive,'pathology_level': p_level,'pathology_valueadded': vig,'subtype': p_sub})
    db.session.commit()
    # 查找到医生判断的结果
    Mri_item = db.session.query(Mri.ai_invasion,Mri.ai_level).filter(Mri.study_id == study_id).all()
    Mri_item=Mri_item[0]

    if Mri_item[0] is None or Mri_item[0] == "":
      inv = ""
    else:
      if Mri_item[0] == p_invasive:
        inv="是"
      else:
        inv="否" 
    
    if Mri_item[1] is None or Mri_item[1] == "":
      men = ""
    else:
      if Mri_item[1] == p_level:
        men="是"
      else:
        men="否"



    db.session.query(Mri).filter_by(study_id=study_id).update({'inv_consistence': inv,'level_consistence': men})
    db.session.commit()
  except Exception as e:
    print(e)
    status=0
  else:
    status=1
  return status